﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SistemaInventarioMVC.Models.Repository;
using SistemaInventarioMVC.Models.Entidades;

namespace SistemaInventarioMVC.Controllers
{
    public class RazonEntradaController : Controller
    {
        private UnitOfWork unitOfWork = null;

        public RazonEntradaController() 
        {
            unitOfWork = new UnitOfWork();
        }
        //
        // GET: /RazonEntrada/

        public ActionResult Index()
        {
            return View(unitOfWork.RazonEntrada.GetAll());
        }

        //
        // GET: /RazonEntrada/Details/5

        public ActionResult Details(int id)
        {
            return View(unitOfWork.RazonEntrada.GetByID(id));
        }

        //
        // GET: /RazonEntrada/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /RazonEntrada/Create

        [HttpPost]
        public ActionResult Create(RazonEntrada razonEntrada)
        {
            try
            {
                unitOfWork.RazonEntrada.Insert(razonEntrada);
                unitOfWork.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("", "Error del sistema. Intentelo de nuevo, sino contacte su adminsitrador de sistemas.");
                return View();
            }
        }
        
        //
        // GET: /RazonEntrada/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View(unitOfWork.RazonEntrada.GetByID(id));
        }

        //
        // POST: /RazonEntrada/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, RazonEntrada razonEntrada)
        {
            try
            {
                unitOfWork.RazonEntrada.Update(razonEntrada);
                unitOfWork.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("", "Error del sistema. Intentelo de nuevo, sino contacte su adminsitrador de sistemas.");
                return View();
            }
        }

        //
        // GET: /RazonEntrada/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(unitOfWork.RazonEntrada.GetByID(id));
        }

        //
        // POST: /RazonEntrada/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, RazonEntrada razonEntrada)
        {
            try
            {
                unitOfWork.RazonEntrada.Delete(id);
                unitOfWork.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("", "Error del sistema. Intentelo de nuevo, sino contacte su adminsitrador de sistemas.");
                return View();
            }
        }
    }
}
